home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / IEditor / Expanders / GetFile / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-17  |  5.7 KB  |  202 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #include "DEV_IE:Expanders/defs.h"
  15.  
  16. extern __geta4 struct Library *LibInit   ( __A0 BPTR );
  17. extern __geta4 struct Library *LibOpen   ( __D0 long, __A0 struct Library * );
  18. extern __geta4 long            LibClose  ( __A0 struct Library * );
  19. extern __geta4 long            LibExpunge( __A0 struct Library * );
  20.  
  21. struct Expander *LibBase = NULL;
  22.  
  23. long SysBase        = NULL;
  24. long DOSBase        = NULL;
  25. long IntuitionBase  = NULL;
  26. long GfxBase        = NULL;
  27. long GadToolsBase   = NULL;
  28. BPTR SegList        = 0;
  29. UBYTE *Desc         = NULL;
  30.  
  31. /*
  32.  *    The Initialization routine is given only a seglist pointer.  Since
  33.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  34.  *    and return either NULL or the library pointer.  Exec has Forbid()
  35.  *    for us during the call.
  36.  */
  37.  
  38. struct Library *LibInit( __A0 BPTR segment )
  39. {
  40.  
  41.     struct Library *lib = NULL;
  42.  
  43.     static const long Vectors[] = {
  44.  
  45.         (long)ALibOpen,         /*  standard lib functions    */
  46.         (long)ALibClose,
  47.         (long)ALibExpunge,
  48.         (long)ALibReserved,
  49.  
  50.         (long)IEX_Mount,        /*  mount function            */
  51.  
  52.         (long)IEX_Add,          /*  edit functions            */
  53.         (long)IEX_Remove,
  54.         (long)IEX_Edit,
  55.         (long)IEX_Copy,
  56.         (long)IEX_Make,
  57.         (long)IEX_Free,
  58.         (long)IEX_Refresh,
  59.  
  60.         (long)IEX_Save,         /*  I/O functions             */
  61.         (long)IEX_Load,
  62.  
  63.         (long)IEX_StartSrcGen,  /*  source related functions  */
  64.         (long)IEX_WriteGlobals, 
  65.         (long)IEX_WriteSetup,
  66.         (long)IEX_WriteCloseDown,
  67.         (long)IEX_WriteHeaders,
  68.         (long)IEX_WriteRender,
  69.         (long)IEX_GetIDCMP,
  70.         (long)IEX_WriteData,
  71.         (long)IEX_WriteChipData,
  72.         (long)IEX_WriteOpenWnd,
  73.         (long)IEX_WriteCloseWnd,
  74.         -1
  75.     };
  76.  
  77.     SysBase = *(long *)4;
  78.  
  79.     DOSBase = (long)OpenLibrary( "dos.library", 36 );
  80.     if(DOSBase!=NULL) {
  81.         IntuitionBase = (long)OpenLibrary( "intuition.library", 36 );
  82.         if(IntuitionBase!=NULL) {
  83.             GfxBase = (long)OpenLibrary( "graphics.library", 36 );
  84.             if( GfxBase !=NULL) {
  85.                 GadToolsBase = (long)OpenLibrary( "gadtools.library", 36 );
  86.                 if(GadToolsBase!=NULL) {
  87.  
  88.                     LibBase = lib =  (long)MakeLibrary( (APTR)Vectors, NULL, NULL, sizeof( struct Expander ), NULL );
  89.                     if(lib!=NULL) {
  90.  
  91.                         lib->lib_Node.ln_Type = NT_LIBRARY;
  92.                         lib->lib_Node.ln_Name = LibName;
  93.                         lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  94.                         lib->lib_Version      = 37;
  95.                         lib->lib_Revision     = 0;
  96.                         lib->lib_IdString     = (APTR)LibId;
  97.  
  98.                         SegList = segment;
  99.  
  100.                         AddLibrary( lib );
  101.                     }
  102.  
  103.                 }
  104.             }
  105.         }
  106.     }
  107.  
  108.     return( lib );
  109. }
  110.  
  111. /*
  112.  *    Open is given the library pointer and the version request.  Either
  113.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  114.  *    Exec has Forbid() for us during the call.
  115.  */
  116.  
  117. struct Library *LibOpen( __D0 long version, __A0 struct Library *lib )
  118. {
  119.     ++lib->lib_OpenCnt;
  120.  
  121.     lib->lib_Flags &= ~LIBF_DELEXP;
  122.  
  123.     return( lib );
  124. }
  125.  
  126. /*
  127.  *    Close is given the library pointer and the version request.  Be sure
  128.  *    not to decrement the open count if already zero.  If the open count
  129.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  130.  *    and return the seglist.  Otherwise we return NULL.
  131.  *
  132.  *    Note that this routine never sets LIBF_DELEXP on its own.
  133.  *
  134.  *    Exec has Forbid() for us during the call.
  135.  */
  136.  
  137. long LibClose( __A0 struct Library *lib )
  138. {
  139.     if( lib->lib_OpenCnt && --lib->lib_OpenCnt )
  140.         return( NULL );
  141.  
  142.     if( lib->lib_Flags & LIBF_DELEXP )
  143.         return( LibExpunge( lib ));
  144.  
  145.     return( NULL );
  146. }
  147.  
  148. /*
  149.  *    We expunge the library and return the Seglist ONLY if the open count
  150.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  151.  *    flag and return NULL.
  152.  *
  153.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  154.  *    might be called from the memory allocator and thus we CANNOT DO A
  155.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  156.  *
  157.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  158.  *    therefore freeze if we called it ourselves.  As far as I can tell
  159.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  160.  *    below.
  161.  */
  162.  
  163. long LibExpunge( __A0 struct Library *lib )
  164. {
  165.     if( lib->lib_OpenCnt ) {
  166.  
  167.         lib->lib_Flags |= LIBF_DELEXP;
  168.         return( NULL );
  169.     }
  170.  
  171.     Remove( &lib->lib_Node );
  172.  
  173.     FreeMem(( char * )lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize );
  174.  
  175.     if( DOSBase ) {
  176.         CloseLibrary( (struct Library *)DOSBase );
  177.         DOSBase = NULL;
  178.     }
  179.  
  180.     if( IntuitionBase ) {
  181.         CloseLibrary( (struct Library *)IntuitionBase );
  182.         IntuitionBase = NULL;
  183.     }
  184.  
  185.     if( GfxBase ) {
  186.         CloseLibrary( (struct Library *)GfxBase );
  187.         GfxBase = NULL;
  188.     }
  189.  
  190.     if( GadToolsBase ) {
  191.         CloseLibrary( (struct Library *)GadToolsBase );
  192.         GadToolsBase = NULL;
  193.     }
  194.  
  195.     if( Desc ) {          /* free the descriptions file  */
  196.         FreeVec( Desc );  /* loaded by IEX_Mount         */
  197.         Desc = NULL;
  198.     }
  199.  
  200.     return(( long )SegList );
  201. }
  202.